<template>
	<view>
			<view class="boot" v-show="tabshow">
				<view class="shoucang bootchild list-icon" @click="isaddfn">
					<text class="list-icon" v-if="!isadd">&#xe71f;</text>
					<text class="list-icon aboutadd" v-else>&#xe666;</text>
				</view>
				<view class="sharebtn bootchild list-icon" @click="showshare">
					<text class="list-icon">&#xe622;</text></view>
			</view>
			<view class="share" v-show="isshowshare">
				<view class="sharebox">
					<!-- <view class="sharetodo" @click="sharewx('WXSceneSession')">微信</view>
					<view class="sharetodo" @click="sharewx('WXSenceTimeline')">微信朋友圈</view> -->
				</view>
				<view class="close" @click="closeshare">关闭</view>
			</view>
	</view>
</template>
<script>
	import {common} from '@/common/publish.js';
	import {mapState,mapMutations} from 'vuex';
	export default {
		data() {
			return {
				webviewStyles: {
                    progress: {
                        color: '#FF3333'
                    }
                },
				url:'',
				isshowshare:false,
				isadd:false,
				w:'',
				view:'',
				olddata:'',
				tabshow:true,
				type:'',
				dataa:{},
				islogin:''
			};
		},
		onHide(){
			console.log('hide')
		},
		onBackPress(){
			this.isshowshare=false;
			this.view.hide();
			console.log(this.isshowshare)
		},
		onLoad(event) {
			var that = this;
			if(event.type){
				that.type = event.type;
			}
			uni.getStorage({
				key:'islogin',
				success(res) {
					that.islogin = res.data;
				}
			})
			uni.getStorage({
				key:'uerInfo',
				success(res) {
					that.userinfo = res.data;
				}
			})
			if(event.type =='proj'){
				this.url = common.localurl+'/api/proForm.htm?'+ event.id;
				this.towebview(this.url)
				this.tabshow = false
			}else if(event.type == 'project'){
				this.getproject(event.id,event.type)
			}else if(event.type == 'projectlist'){
				this.getprojectlist(event.id,event.type)
			}else if(event.type == 'userto'){
				this.url = event.url
				this.tabshow = false
				this.towebview(this.url)
			}else if(event.type == 'index'){
				this.getindex(event.id,event.type)
			}else if(event.type == 'qyzt'){
				this.getqyzt(event.id,event.type)
			}else{
				if(event.otrcan){
					var xinnew = event.otrcan.replace(/---/g,"&").replace(/--/g,"=")
					this.url = event.url+'?'+xinnew
					this.towebview(this.url)
				}else{
					this.url = event.url
					this.towebview(this.url)
				}
				
			}
			this.view = new plus.nativeObj.View('test',{bottom:'50px',left:'0px',height:'88px',width:'100%'});
			// 绘制文本
			this.view.drawRect("#cccccc", {top:'0px',left:'0px',width:'100%',height:'1px'});
			this.view.drawRect("#ffffff", {top:'0px',left:'0px',width:'100%',height:'100%'});
			this.view.drawRect("#cccccc", {top:'44px',left:'0px',width:'100%',height:'1px'});
			this.view.drawText('微信', {bottom:'44px',left:'0px',width:'100%',height:'44px'}, {size:'16px'});
			this.view.drawText('微信朋友圈', {bottom:'4px',left:'0px',width:'100%',height:'44px'}, {size:'16px'});	
		},
		methods: {
			isaddfn(){
				var that = this;
				if(that.islogin){
					if(this.isadd){
						//取消
						that.isadd = false;
						uni.request({
							url:common.csurl+'/api/collect/cancel.json',
							data:{
								unionId:that.userinfo.unionId,
								type:that.type,
								collectId:that.dataa.id
							},
							success(res) {
								if(res.data){
									that.isadd = false;
								}
							}
						})
						
					}else{
						//收藏
						that.isadd = true;
						uni.request({
							url:common.csurl+'/api/collect/save.json',
							data:{
								unionId:that.userinfo.unionId,
								type:that.type,
								collectId:that.dataa.id,
								title:that.dataa.title,
								picture:that.dataa.picture,
								web :1
							},
							success(res) {
								console.log(res.data)
								if(res.data){
									that.isadd = true;
								}
							}
						})
					}
				}else{
					uni.showToast({
						title: '收藏需要登陆',
						icon:'none',
						duration: 1500
					});
				}
			},
			towebview(val){
				var that = this;
				var pages = getCurrentPages();  
				var page = pages[pages.length - 1];  
				var currentWebview = getCurrentPages();
				// var currentWebview = page.$getAppWebview();
				uni.getSystemInfo({
					success: function (res) {
						that.w = plus.webview.create(val,'web1',{
    position: 'static', zIndex:-1,top:0,bottom:60*res.windowWidth/375,width:res.windowWidth,height:res.windowHeight-60*res.windowWidth/375,scalable:true});
						currentWebview.append(that.w)
					}
				});
			},
			getproject:function(id,who){
				uni.request({
					url: common.localurl+'/api/project/project.json', 
					data:{projectId:id},
					method:'GET',
					dataType:'json',
					success: (res) => {
						this.url = res.data.data.project.link;
						this.towebview(this.url)
						this.dolishi(this,res.data.data.project,who,true)
					}
				});
			},
			getprojectlist:function(id,who){
				uni.request({
					url: common.localurl+'/api/project/projectDynamic.json', 
					data:{projectDynamicId:id},
					method:'GET',
					dataType:'json',
					success: (res) => {
						this.url = res.data.link;
						this.towebview(this.url)
						this.dolishi(this,res.data,who,true)
					}
				}); 
			},
			getindex(id,who){
				uni.request({
					url: common.csurl2+'/api/newsCentre/homePageNews.json', 
					data:{id:id},
					method:'GET',
					dataType:'json',
					success: (res) => {
						this.url = res.data.link;
						var newdata = res.data;
						var link = newdata.picture
						var	reg = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/g;
						var wherefrom = reg.test(link)
						if(!wherefrom&&link){
							newdata.picture=common.csurl2+newdata.picture
						}
						this.towebview(this.url)
						this.dolishi(this,newdata,who,true)
					}
				});
			},
			getqyzt(id,who){
				uni.request({
					url: common.csurl2+'/api/Frontiers/detail.json', 
					data:{frontiersId:id},
					method:'GET',
					dataType:'json',
					success: (res) => {
						this.url = res.data.data.link;
						var newdata = res.data.data;
						var link = newdata.picture
						var	reg = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/g;
						var wherefrom = reg.test(link)
						if(!wherefrom&&link){
							newdata.picture=common.csurl2+newdata.picture
						}
						this.towebview(this.url)
						this.dolishi(this,newdata,who,true)
					}
				});
			},
			dolishi(that,data,who,isweb){
				that.dataa = data;
				uni.getStorage({
					key:'uerInfo',
					success(res) {
						if(res.data){
							that.isshoucang(who,data.id,res.data.unionId)
						}
					}
				})
				uni.getStorage({
					key:'lishi',
					success(res) {
						var addarr = {key:data,who:who,isweb:isweb};
						//判定是否存在此条信息
						var isok = true;
						for (var s=0;s<res.data.length;s++) {
							if(res.data[s].key.id == data.id){
								res.data.splice(s,1)
								isok = false;
								res.data.unshift(addarr)
								break;
							}
						}
						if(isok){
							res.data.unshift(addarr)
							if(res.data.length>100){
								res.data.pop()
							}
						}
						uni.setStorage({
							key:'lishi',
							data:res.data,
							success(res) {
								//console.log(res.data)
							}
						})
					},
					fail(res) {
						uni.setStorage({
							key:'lishi',
							data:[{key:data,who:who,isweb:isweb}],
							success() {
								
							}
						})
					}
				})
			},
			sharewx(who){
				var that = this;
				uni.getProvider({
					service: 'share',
					success: function (res) {
						console.log(JSON.stringify(res));
						if (~res.provider.indexOf('weixin')) {
							uni.share({
								provider: "weixin",
								scene: who,
								type: 0,
								href: that.url,
								title: '教育之弦',
								summary: '教育之弦',
								imageUrl: common.localurl+"/assets/front/image/jyzxlogo.jpg",
								success: function (res) {
									//console.log("success:" + JSON.stringify(res));
									that.isshowshare =false;
								},
								fail: function (err) {
									//console.log("fail:" + JSON.stringify(err));
								}
							});
						}
					}
				});
				
			},
			closeshare(){
				this.isshowshare =false;
				this.view.hide();
			},
			showshare(){
				
				this.view.show();
				this.isshowshare =true;
				this.view.addEventListener("click", this.onClick, false);
				this.olddata = '';
			},
			// 监听事件函数
			onClick(e){
				var newdata = new Date().getTime();
				if(newdata-this.olddata>500||!this.olddata){
					console.log("点击原生控件："+JSON.stringify(e));
					var clientY = e.clientY;
					if(clientY<44){
						console.log('wx')
						this.sharewx('WXSceneSession')
					}else{
						console.log('py')
						this.sharewx('WXSenceTimeline')
					}
					this.view.hide();
				}
				this.olddata = new Date().getTime()
			},
			isshoucang(who,collectId,unionId){
				var that = this;
				uni.request({
					url:common.csurl+'/api/collect/collectDetail.json',
					data:{
						type:who,
						collectId:collectId,
						unionId:unionId
					},
					success(res) {
						if(res.data){
							that.isadd = true;
						}
					}
				})
			}
		},
	}
</script>

<style lang="scss">
	@import '@/common/publish.scss';
	@font-face {
		font-family: texticons;
		font-weight: normal;
		font-style: normal;
		src: url('../../static/font/iconfont.ttf') format('truetype');
	}
	page{
		background-color: $bgcolor;
	}
	.boot{
		position:fixed;
		left:0;
		bottom:0;
		width:750upx;
		height:120upx;
		display: flex;
		flex-direction:row;
		flex-wrap:nowrap ;
		justify-content:space-between;
		align-items:flex-start;
		line-height:120upx;
		background-color:#fff;
		border-top:1px solid #fbfbfb;
		z-index:10;
	}
	.bootchild{
		text-align:center;
		width:50%;
	}
	.share{
		position:fixed;
		left:0;
		bottom:0;
		height:300upx;
		width:750upx;
		background-color:#fff;
		border-top:1px solid #fbfbfb;
		z-index:11;
	}
	.sharebox{
		width:100%;
	}
	.sharetodo{
		width:750upx;
		height:100upx;
		line-height:100upx;
		font-size:32upx;
		color:$fontblue;
		text-align: center;
		border-bottom:1px solid #ccc;
	}
	.list-icon {
		width: 300upx;
		height: 120upx;
		line-height: 114upx;
		font-size: 46upx;
		color: $fontblue;
		text-align: center;
		font-family: texticons;
		margin-right: 20upx;
		display:inline-block;
		margin:0 auto;
	}
	.aboutadd{
		font-size:44upx;
	}
	.close{
		text-align: center;
		color:$fontblue;
		position: absolute;
		bottom:0;
		left:0;
		width:750upx;
		height:100upx;
		font-size:32upx;
		line-height:100upx;
		background-color:$bgcolor;
	}
</style>
